<?php
/**
 * Created by PhpStorm.
 * User: asdf
 * Date: 2021/9/6
 * Time: 13:15
 */

namespace Extentions\upload;
use function core\p;

class Upload{
    private static $instance;
    public $fsrc;
    public $allowedExts = array("gif", "jpeg", "jpg", "png","zip","rar","doc");
    public $ext;
    public $name;
    public $mine;
    public $checkmine = false;
    private $tempname;
    private $size;
    public $up_path;
	private $raw;
	public $invalid = [];
	public $up_file_number_limit = 1;


    public static function Newinstance($up_path = ""){
        if(!self::$instance instanceof self){
            self::$instance = new self($up_path);
        }
        return self::$instance;
    }


	private function __construct($up_path = "")
    {
        if ("" === trim($up_path)){
            throw new \ErrorException("not difine filepath");
        }
        $this->up_path=$up_path;
        $fsrc = is_array($this->fsrc)?$this->fsrc:$_FILES;
        $this->fileinfo($fsrc);
    }


    function SetFilename($name){
        $this->name = $name;
    }

    function Set_up_file_number_limit($limit){
	    $this->up_file_number_limit = $limit;
    }

    function SetValidMineType($mine){
	    $this->mine = $mine;
    }

    function SetValidExt($ext){
        $this->ext = $ext;
    }

    private function fileinfo($file){
        $this->raw=$file;
        $this->name=$this->raw["file"]["name"];
        if(!is_array($this->name))
            $this->ext=end(explode(".", $this->raw["file"]["name"]));
        $this->mine=$this->raw["file"]["type"];
        $this->tempname=$this->raw["file"]["tmp_name"];
        $this->size=$this->raw["file"]["size"];
    }

    private function CheckMineType($mine){
        if(in_array($mine,$this->allowedExts)){
            return true;
        }else{
            return false;
        }
    }

    private function CheckExtention($ext){
        if(in_array($ext,$this->allowedExts)){
            return true;
        }else{
            return false;
        }
    }

    private function check($extention = "",$mine = ""){
	    if ($this->checkmine){
            $mine = $this->CheckMineType($mine);
        }else{
            $mine = true;
        }

        if ($this->CheckExtention($extention) && $mine){
	        return true;
        }else{
	        return false;
        }
    }

    private function move($src,$des){
        move_uploaded_file($src,$des);
    }


    private function ext($str){
        return end(explode(".", $str));
    }


    function uploadmf($path = ""){
        $arr = array();
        if(trim($path)!=="") $this->up_path = $path;
        $i = 0;
        foreach ($this->name as $value){
            if ($this->check($this->ext($value))){
                $this->move($this->tempname[$i],$this->up_path.$value);
            }else{
                $arr[] = $value;
            }
            if ($i===$this->up_file_number_limit){
                break;
            }
            $i++;
        }
        $this->invalid=$arr;
    }


    function uploadf($path = ""){
        if(trim($path)!=="") $this->up_path = $path;
        if ($this->check($this->ext)){
//            p($this->up_path.$this->name);
            $this->move($this->tempname,$this->up_path.$this->name);
        }else{
            die("null");
        }
    }

}


?>